package com.bamboo.tool.template.torna.config;

import cn.hutool.core.util.StrUtil;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.template.torna.entity.LoginToken;
import com.bamboo.tool.template.torna.entity.TornaRespBase;
import com.bamboo.tool.template.torna.entity.TornaSetting;
import com.bamboo.tool.template.torna.util.TornaRequestUtil;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TornaConfigurable implements Configurable {
    private JTextField tornaUrlField;
    private JLabel tornaUrlLabel;
    private JPanel mainPanel;
    private JTextField usernameField;
    private JLabel userNameField;
    private JLabel passwordLabel;
    private JButton loginButton;
    private JLabel loginLabel;
    private JPasswordField passwordField;
    private JPanel lohinPanel;

    @Override
    public String getDisplayName() {
        return "Torna Setting";
    }

    @Override
    public @Nullable JComponent createComponent() {

        TornaSetting state = RestfulCloudApplicationCache.getInstance().getState().getTornaSetting();

        tornaUrlField.setText(state.getUrl());
        usernameField.setText(state.getUserName());
        passwordField.setText(state.getPassword());
        //url listenter
        urlChangeListener(state);
        // username listenter
        usernameChangeListener(state);
        //password listenter
        passwordChangeListener(state);
        // longin listenter
        loginListener(state);
        try {
            TornaRespBase<LoginToken> login = TornaRequestUtil.login(state);
            if (login.isOk()) {
                state.setToken(login.getData().getToken());
                loginLabel.setForeground(Color.GREEN);
                loginLabel.setText("success!");
            } else {
                loginLabel.setForeground(Color.red);
                loginLabel.setText(login.getMsg());
            }
        } catch (Exception exception) {
            loginLabel.setForeground(Color.red);
            loginLabel.setText(exception.getMessage());
        }
        return mainPanel;
    }

    private void loginListener(TornaSetting state) {
        loginButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (StrUtil.isEmpty(tornaUrlField.getText())) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("torna url is not null");
                    return;
                }

                if (StrUtil.isEmpty(usernameField.getText())) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("username is not null");
                    return;
                }
                char[] passwordChar = passwordField.getPassword();
                String password = new String(passwordChar);
                if (StrUtil.isEmpty(password)) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText("password is not null");
                    return;
                }

                state.setUrl(tornaUrlField.getText());
                state.setUserName(usernameField.getText());
                state.setPassword(password);
                try {
                    TornaRespBase<LoginToken> login = TornaRequestUtil.login(state);
                    if (login.isOk()) {
                        state.setToken(login.getData().getToken());
                        loginLabel.setForeground(Color.GREEN);
                        loginLabel.setText("success!");
                    } else {
                        loginLabel.setForeground(Color.red);
                        loginLabel.setText(login.getMsg());
                    }
                } catch (Exception exception) {
                    loginLabel.setForeground(Color.red);
                    loginLabel.setText(exception.getMessage());
                }


            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    private void urlChangeListener(TornaSetting state) {
        tornaUrlField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                state.setUrl(tornaUrlField.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                state.setUrl(tornaUrlField.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                state.setUrl(tornaUrlField.getText());
            }
        });
    }

    private void usernameChangeListener(TornaSetting state) {
        usernameField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                state.setUserName(usernameField.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                state.setUserName(usernameField.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                state.setUserName(usernameField.getText());
            }
        });
    }

    private void passwordChangeListener(TornaSetting state) {
        passwordField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                char[] password = passwordField.getPassword();
                state.setPassword(new String(password));
            }
        });
    }

    @Override
    public boolean isModified() {

        return false;
    }

    @Override
    public void apply() throws ConfigurationException {

    }
}
